<?php
/* 
* [用户表管理模型]
* @author: mazhenyu[houdunwangmzy@163.com]
* @Date:   2014-06-16 14:57:15
* @Last Modified time: 2014-06-30 10:43:59
*/
class UserModel extends Model{
	//指定表名
	public $table =	'user';


	/**
	 * [validate_login 验证登录]
	 * @return [type] [description]
	 */
	public function validate_login($type){
		if($type=="index"){
				//接收用户提交数据
			$username = Q('post.username');
			$password = Q('post.password','','md5');
	
			//用户名是否存在
			//用户名密码是否正确
			//判断是否是后台管理员
			$userData = $this->where(array('username'=>$username))->find();
			if(!$userData || $password != $userData['password']){
				$this->error = '用户名或者密码错误';
				return false;
			}
		
			//如果走到下面证明，通过上面重重关卡
			return $userData;
		}	
		$code = Q('post.code','','strtoupper');
		//判断验证码是否正确
//		if($code != session('code')){
//			$this->error = '验证码错误';
//			return false;
//		}
		//接收用户提交数据
		$username = Q('post.username');
		$password = Q('post.password','','md5');

		//用户名是否存在
		//用户名密码是否正确
		//判断是否是后台管理员
		$userData = $this->where(array('username'=>$username))->find();
		if(!$userData || $password != $userData['password'] || !$userData['is_admin']){
			$this->error = '用户名或者密码错误';
			return false;
		}
		//是否锁定
		if($userData['is_lock'] == 1){
			$this->error = '用户已被锁定，请联系管理员';
			return false;
		}
	
		//如果走到下面证明，通过上面重重关卡
		return $userData;

	}

	public function _add($data){
		return $this->add($data);
	}
	public function check_user($username){
		if($this->where(array('username'=>$username))->getField('uid')){
			return true;
		}
		return false;
	}
		/**
	 * [get_one 获得一条记录]
	 * @param  [type] $where [description]
	 * @return [type]        [description]
	 */
	public function get_one($where,$field=null){
		return $this->where($where)->field($field)->find();
	}
	//QQ_login info
	public function get_user_info_qq($where,$field=null){
		
		return $this->where($where)->field($field)->find();
	}
}
?>
